//获取所有ajax接口地址
var config = require('utils/config.js');
//app.js
App({
  onLaunch: function (options) {
    console.log("打开小程序的场景值" + options.scene);
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    /*
      缓存设置 初始化
      isLogin         登录状态
    */
    wx.setStorageSync("isLogin", false);
    //缓存 header的sessionid，用来登录后接口请求
    wx.setStorageSync("storage_SESSION", "");

  },
  onShow: function (options) {
    // 当小程序启动，或从后台进入前台显示，会触发 onShow
    //this.logout();
  },
  onHide: function () {
    // 当小程序从前台进入后台，会触发 onHide
    //this.logout();
  },
  onError: function (msg) {
    //当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
    console.log(msg)
  },
  globalData: {
    userInfo: null
  },
  config: config,  //挂载所有ajax接口地址
  //登录成功后的跳转
  loginSkip: function (data) {
    var that = this;
    //登录处理
    wx.setStorageSync("isLogin", true);
    // JSessionID
    wx.setStorageSync("storage_SESSION", JSessionID);

  },
  logout: function () {
    var that = this;
    wx.showModal({
      title: '',
      content: '确定要退出吗？',
      confirmColor: "#2e8ddf",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          //退出登录，移除缓存数据
          wx.setStorageSync("isLogin", false);
          //缓存 header的sessionid，用来登录后接口请求
          wx.setStorageSync("storage_SESSION", "");

        } else if (res.cancel) {
          console.log('用户点击取消')

        }
      }
    })

  },
  config: config,  //挂载所有ajax接口地址
  ajaxGet: function (objs) {//get ajax
    var dataType = objs.dataType ? objs.dataType : "json";
    try {
      var storage_SESSION = wx.getStorageSync('storage_SESSION') != "" ? 'JSESSIONID=' + wx.getStorageSync('storage_SESSION') : "";
      // Do something with return value
      wx.request({
        url: objs.url,
        method: 'GET',
        data: objs.data,
        header: {
          'content-type': 'application/json',
          'cookie': storage_SESSION
        },
        dataType: dataType,
        success: function (res) {
          objs.success(res.data, res.statusCode, res.header)
        },
        fail: function (res) {
          objs.fail(res.data, res.statusCode, res.header)
        },
        complete: function (res) {
          objs.complete(res.data, res.statusCode, res.header)
        }
      });
    } catch (e) {
      // Do something when catch error
    };

  },
  ajaxPost: function (objs) {//post ajax
    var dataType = objs.dataType ? objs.dataType : "json";
    try {
      var storage_SESSION = wx.getStorageSync('storage_SESSION') != "" ? 'JSESSIONID=' + wx.getStorageSync('storage_SESSION') : "";
      // Do something with return value
      wx.request({
        url: objs.url,
        method: 'POST',
        data: objs.data,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': storage_SESSION
        },
        dataType: dataType,
        success: function (res) {
          objs.success(res.data, res.statusCode, res.header)
        },
        fail: function (res) {
          objs.fail(res.data, res.statusCode, res.header)
        },
        complete: function (res) {
          objs.complete(res.data, res.statusCode, res.header)
        }
      });
    } catch (e) {
      // Do something when catch error
    };

  },
  fileUpload: function (objs) {//upload
    try {
      var storage_SESSION = wx.getStorageSync('storage_SESSION') != "" ? 'JSESSIONID=' + wx.getStorageSync('storage_SESSION') : "";
      // Do something with return value
      wx.uploadFile({
        url: objs.url,
        filePath: objs.filePath,
        name: objs.name,
        formData: objs.formData,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': storage_SESSION
        },
        success: function (res) {
          objs.success(JSON.parse(res.data), res.statusCode, res.header)
        },
        fail: function (res) {
          objs.fail(JSON.parse(res.data), res.statusCode, res.header)
        },
        complete: function (res) {
          objs.complete(JSON.parse(res.data), res.statusCode, res.header)
        }
      });
    } catch (e) {
      // Do something when catch error
    };

  }
})
